package com.example.video;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class VideoListActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private VideoAdapter videoAdapter;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_list);
        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        // 获取本地视频列表
//        List<Video> videoList = getLocalVideos();
        List<Video> videoList = new ArrayList<>();
        Video video = new Video("标题一","/storage/self/primary/images/test.mp4");
        videoList.add(video);
        videoList.add(video);
        videoList.add(video);
        videoList.add(video);
        videoList.add(video);
        videoList.add(video);
        videoList.add(video);
        videoList.add(video);
        videoList.add(video);
        videoList.add(video);
        videoList.add(video);
        videoList.add(video);
        videoList.add(video);
        videoList.add(video);
        videoList.add(video);
        videoList.add(video);
        videoList.add(video);
        videoList.add(video);
        videoList.add(video);
        videoList.add(video);
        videoList.add(video);
        videoList.add(video);
        videoList.add(video);
        videoList.add(video);
        videoList.add(video);
        videoList.add(video);
        videoList.add(video);


        videoAdapter = new VideoAdapter(videoList);
        recyclerView.setAdapter(videoAdapter);
    }

    // 获取本地视频列表
/*    private List<Video> getLocalVideos() {
        List<Video> videos = new ArrayList<>();
        // 从存储中获取视频文件，并将视频信息加入到 videos 列表中
        // 这里需要读取存储权限，然后使用 ContentResolver 或 File 相关 API 来获取视频信息
        return videos;
    }*/
    private List<Video> getLocalVideos() {
        List<Video> videos = new ArrayList<>();

//        File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Videos");
        File folder = new File("/storage/self/primary/DCIM/Camera");

        if (folder.exists() && folder.isDirectory()) {
            File[] files = folder.listFiles();

            if (files != null) {
                for (File file : files) {
                    if (file.isFile() && isVideoFile(file)) {
                        String title = file.getName();
                        String filePath = file.getAbsolutePath();

                        Video video = new Video(title, filePath);
                        videos.add(video);
                    }
                }
            }
        }

        return videos;
    }

    private boolean isVideoFile(File file) {
        String name = file.getName();
        String extension = name.substring(name.lastIndexOf(".") + 1);
        String[] videoExtensions = {"mp4", "avi", "mkv", "mov"};

        for (String videoExtension : videoExtensions) {
            if (extension.equalsIgnoreCase(videoExtension)) {
                return true;
            }
        }

        return false;
    }


}
